package com.sky.happygos.ui.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.sky.happygos.R;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.FailOrderE;
import com.sky.happygos.event.FinishOrderE;
import com.sky.happygos.event.OrderStatusOneE;
import com.sky.happygos.event.ReceiveOrderE;
import com.sky.happygos.model.Order;
import com.sky.happygos.present.OrderP;
import com.sky.happygos.ui.activity.adapter.TransAdapter;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qqqqqqq o17/4/14.
 */
@EFragment(R.layout.fragment_transaction)
public class TransactionF extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    @ViewById(R.id.rv_shop)
    RecyclerView recyclerView;
    @ViewById(R.id.loadView)
    LoadView loadView;
    @ViewById(R.id.swipe_container)
    SwipeRefreshLayout swipeRefreshLayout;

    @Bean
    OrderP orderP;

    List<Order> orders;

    TransAdapter transAdapter;

    @AfterViews
    public void init(){
        swipeRefreshLayout.setOnRefreshListener(this);

        orders = new ArrayList<>();
        transAdapter = new TransAdapter(getContext(),orders);
        transAdapter.setMyItemClickListener(new TransAdapter.MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {

            }

            @Override
            public void finishOrderClick(int position) {
                Order order = orders.get(position);
                loadView.start();
                orderP.finishOrder(order.id,0);
            }

            @Override
            public void failOrderClick(int position) {
                Order order = orders.get(position);
                loadView.start();
                orderP.failOrder(order.id,0);
            }

            @Override
            public void receiveOrderClick(int position) {
                Order order = orders.get(position);
                loadView.start();
                orderP.receiveOrder(order.id,0);
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(transAdapter);

        loadView.start();
        orderP.getOrders(0);
    }

    @Subscribe
    public void listOrderResult(OrderStatusOneE e){
        if(e.type != 0){
            return;
        }
        loadView.dismiss();
        swipeRefreshLayout.setRefreshing(false);
        if(e.isSuccess){
            orders = e.data;
            transAdapter.list = orders;
            transAdapter.notifyDataSetChanged();
        }else{
            Toast.makeText(getContext(),"请求失败："+e.error.message,Toast.LENGTH_SHORT).show();
        }
    }
    @Subscribe
    public void finishOrderResult(FinishOrderE e){
        if(e.type != 0){
            return;
        }
        if(e.isSuccess){
            orderP.getOrders(0);
        }else{
            Toast.makeText(getContext(),"请求失败："+e.error.message,Toast.LENGTH_SHORT).show();
            loadView.dismiss();
        }
    }

    @Subscribe
    public void failOrderResult(FailOrderE e){
        if(e.type != 0){
            return;
        }
        if(e.isSuccess){
            orderP.getOrders(0);
            Toast.makeText(getContext(),"操作成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getContext(),"请求失败："+e.error.message,Toast.LENGTH_SHORT).show();
            loadView.dismiss();
        }
    }

    @Subscribe
    public void receiveOrderResult(ReceiveOrderE e){
        if(e.type != 0){
            return;
        }
        if(e.isSuccess){
            orderP.getOrders(0);
            Toast.makeText(getContext(),"接受订单成功",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(getContext(),"请求失败："+e.error.message,Toast.LENGTH_SHORT).show();
            loadView.dismiss();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onRefresh() {
        orderP.getOrders(0);
    }
}
